package com.xumt.hwHMSdemo.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.xumt.hwHMSdemo.CommodityActivity;
import com.xumt.hwHMSdemo.Commodity_message;
import com.xumt.hwHMSdemo.R;
import com.xumt.hwHMSdemo.ShopActivity;
import com.xumt.hwHMSdemo.Shoppingcard;
import com.xumt.hwHMSdemo.cannectSb.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShopingFragment extends Fragment {
    private String shoplistresult=null;
    private SimpleAdapter shopadapter = null;
    public static double clicklatitude=0;
    public static double clicklongitude=0;
    private List<Map<String, Object>> shoplist = new ArrayList<Map<String, Object>>();
    public  static int clickshop=0;
    public static  String clickshopname=null;
    public static  String clickshopplace=null;
    private String name;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.activity_shop, container, false);
        shopadapter = new MySimpleAdapter(getActivity(), getData(), R.layout.shoplist, new String[]{"shopname", "shopmessage", "shopplace","shopimage"},
                new int[]{R.id.shopname, R.id.shopmessage, R.id.shopplace,R.id.objectimage});
        ListView listView = (ListView) root.findViewById(R.id.shoplist);
        listView.setAdapter(shopadapter);
        AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, Object> map = shoplist.get(position);
                clickshop = (int) map.get("shopid");
                clicklatitude = Double.valueOf(map.get("shoplatitude").toString());
                clicklongitude = Double.valueOf(map.get("shoplongitude").toString());
                clickshopname = (String) map.get("shopname");
                clickshopplace = (String) map.get("shopplace");
                Intent intent = new Intent(getActivity(), CommodityActivity.class);
                startActivity(intent);
            }
        };
        listView.setOnItemClickListener(itemClickListener);
        return root;
    }
    private String shoplistdata() {
        new Thread() {
            public void run() {
                try {
                    HttpRequest request = new HttpRequest();
                    shoplistresult = request.sendGet("http://169.254.67.221:8081/shop/findallshop","");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        return shoplistresult;
    }

    private List<Map<String, Object>> getData() {
        try {
            shoplistdata();
            Thread.sleep(500);
            JSONArray jsonArray = new JSONArray(shoplistresult);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("shopname", "商店：" + jsonObject.getString("shopname"));
                map.put("shopmessage", "简介：" + jsonObject.getString("shopmessage"));
                map.put("shopplace", "位置：" + jsonObject.getString("shopplace"));
                map.put("shoplatitude", jsonObject.getString("shoplatitude"));
                map.put("shoplongitude", jsonObject.getString("shoplongitude"));
                map.put("shopid",  jsonObject.getInt("id"));
                Context ctx=getActivity().getBaseContext();
                int resId = getResources().getIdentifier(jsonObject.getString("shopimage"), "drawable" ,ctx.getPackageName());
                map.put("shopimage",resId);
                shoplist.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return shoplist;
    }

    private class MySimpleAdapter extends SimpleAdapter {

        public MySimpleAdapter(Context context,
                               List<? extends Map<String, ?>> data, int resource,
                               String[] from, int[] to) {
            super(context, data, resource, from, to);
            // TODO Auto-generated constructor stub
        }
    }
}